package net.valerijtimakov.homefinances.dao.storages.finances.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.valerijtimakov.homefinances.domain.common.CathegoryItem;
import net.valerijtimakov.homefinances.domain.common.CathegoryType;
import net.valerijtimakov.homefinances.domain.common.DataItem;
import net.valerijtimakov.homefinances.exceptions.AppExceptionWrongState;
import net.valerijtimakov.homefinances.exceptions.AppPersProviderException;
import net.valerijtimakov.homefinances.exceptions.AppPersistentException;
import net.valerijtimakov.homefinances.exceptions.wrongarg.AppExceptionNullArgument;
import net.valerijtimakov.homefinances.dao.storages.common.mysql.OwnedNamedItemsStorage;
import net.valerijtimakov.homefinances.dao.storages.finances.ICathegoryStorage;

public class CathegoryStorage extends OwnedNamedItemsStorage implements ICathegoryStorage {

	protected String superCathIdFieldName = "supercath_id";
	protected String typeFieldName = "type";
	
	public CathegoryStorage(IMySqlProvider provider) throws AppExceptionWrongState {
		super(provider);
	}
	
	public Map<Long, CathegoryItem> getAllUserCathegories(long userId) 
		throws AppPersProviderException, AppExceptionWrongState 
	{
		return getAllUserItems(userId);
	}

	public Map<Long, CathegoryItem> getCathegories(long ... id) 
		throws AppPersProviderException, AppExceptionWrongState 
	{
		return getItemsByIds(id);
	}

	public void addCathegory(CathegoryItem item) throws AppExceptionNullArgument, 
		AppExceptionWrongState, AppPersistentException 
	{
		addItem(item, "Не задан экземпляр категории!", "Ошибка добавления категории!");
	}

	public void removeCathegory(long ... id) throws AppPersProviderException, AppExceptionWrongState {
		delete(id);
	}

	public void updateCathegory(CathegoryItem item) throws AppPersProviderException, AppExceptionWrongState {
		update(item);
	}

	@Override
	protected String getTableName() {
		return "cathegory";
	}

	@Override
	protected DataItem createTempItem() {
		return new CathegoryItem();
	}

	@Override
	protected <T extends DataItem> void parse(ResultSet rs, T item) throws SQLException {
		super.parse(rs, item);
		CathegoryItem cItem = (CathegoryItem)item;
		cItem.setSuperId(rs.getLong(superCathIdFieldName));
		cItem.setType(CathegoryType.valueOf(rs.getString(typeFieldName)));
	}
	
	@Override
	protected void initSelect() {
		super.initSelect();
		selectQuery.addField(superCathIdFieldName);
		selectQuery.addField(typeFieldName);
	}

	@Override
	protected <T extends DataItem> void addData(T item) {
		super.addData(item);
		CathegoryItem cItem = (CathegoryItem)item;
		insertQuery.setFieldValue(superCathIdFieldName, cItem.getSuperId());
		insertQuery.setFieldValue(typeFieldName, cItem.getType());
	}

	@Override
	protected <T extends DataItem> void setUpdatedFields(T item) {
		super.setUpdatedFields(item);
		CathegoryItem cItem = (CathegoryItem)item;
		updateQuery.setFieldValue(superCathIdFieldName, cItem.getSuperId());
		updateQuery.setFieldValue(typeFieldName, cItem.getType());
	}
}
